/**
 * 
 */
package com.ydy.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ydy.annotation.CheckFormRepeat;
import com.ydy.annotation.CtrlParam;
import com.ydy.annotation.StaffToken;
import com.ydy.annotation.UserToken;
import com.ydy.controller.base.BaseController;
import com.ydy.dto.BillDTO;
import com.ydy.exception.BusinessException;
import com.ydy.ienum.EnumSystem;
import com.ydy.model.Order;
import com.ydy.service.order.OrderService;
import com.ydy.utils.StringUtils;
import com.ydy.vo.other.BaseVo;
import com.ydy.vo.other.PageVo;

/**
 * @author xuzhaojie
 *
 *         2019年1月18日 上午9:17:29
 */
@Controller
@RequestMapping(value = "order", name = "订单模块")
public class OrderController extends BaseController {

	@Autowired
	private OrderService orderService;

	@StaffToken
	@GetMapping(value = "select", name = "店员筛选订单列表")
	@ResponseBody
	public ResponseEntity<PageVo<Order>> select(Order order, String word, Date startTime, Date endTime, Integer page,
			Integer size) {
		StringUtils.setParamEmptyToNull(order);
		PageVo<Order> vo = orderService.select(order, getStaff(), word, startTime, endTime, page, size);
		return ResponseEntity.ok(vo);
	}

	@UserToken
	@GetMapping(value = "selectUserOrder", name = "用户筛选订单列表")
	@ResponseBody
	public ResponseEntity<PageVo<Order>> selectUserOrder(Order order, String word, Date startTime, Date endTime,
			Integer page, Integer size) {
		StringUtils.setParamEmptyToNull(order);
		PageVo<Order> vo = orderService.selectUserOrder(order, getUser(), word, startTime, endTime, page, size);
		return ResponseEntity.ok(vo);
	}

	@UserToken
	@PostMapping(value = "bill", name = "计算账单")
	@ResponseBody
	public ResponseEntity<BillDTO> bill(@RequestBody BillDTO dto) {
		return ResponseEntity.ok(orderService.calculateBill(dto));
	}

	@UserToken
	@CheckFormRepeat
	@PostMapping(value = "create", name = "创建订单")
	@ResponseBody
	public ResponseEntity<Order> create(@RequestBody Order order, @CtrlParam("验证码") String code,
			HttpServletRequest request) {
		if (!compareCode(code, request)) {
			throw new BusinessException(EnumSystem.CODE_ERROR);
		}
		order = orderService.createOrder(order, getUser());
		removeCode(request);
		return ResponseEntity.ok(order);
	}

	@StaffToken
	@GetMapping(value = "selectById", name = "店员查看订单详情")
	@ResponseBody
	public ResponseEntity<Order> selectById(@CtrlParam("订单ID") Long orderId) {
		return ResponseEntity.ok(orderService.selectById(orderId, getStaff()));
	}

	@UserToken
	@GetMapping(value = "selectUserOrderById", name = "用户查看订单详情")
	@ResponseBody
	public ResponseEntity<Order> selectUserOrderById(@CtrlParam("订单ID") Long orderId) {
		return ResponseEntity.ok(orderService.selectByIdAndUser(orderId, getUser()));
	}

	@UserToken
	@GetMapping(value = "close", name = "用户关闭订单")
	@ResponseBody
	public ResponseEntity<BaseVo> close(@CtrlParam("订单ID") Long orderId) {
		return ResponseEntity.ok(orderService.updateOrderStatusClose(orderId, getUser()));
	}

	@UserToken
	@DeleteMapping(value = "delete", name = "用户删除订单")
	@ResponseBody
	public ResponseEntity<BaseVo> delete(@CtrlParam("订单ID") Long orderId) {
		return ResponseEntity.ok(orderService.delete(orderId));
	}

}